﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace dotNails.CodeGen.Util {
	public class StringUtil {
		public static string Pluralize(string input) {
			string output;

			// TODO: Replace with something more intelligent
			if (input.EndsWith("s")) {
				output = input + "es";
			}
			else {
				output = input + "s";
			}

			return output;
		}
		public static string LowerCaseFirstLetter(string value) {
			return value.Substring(0, 1).ToLower() + value.Substring(1);
		}

		public static string GetCamelCaseName(string value) {
			string temp = GetPascalCaseName(value);
			return LowerCaseFirstLetter(temp);
		}

		public static string GetPascalCaseName(string value) {

			string temp = value;
			if (temp.IndexOf("_", 0, temp.Length) > 0) {
				temp = value.ToLower();

				temp = temp.Substring(0, 1).ToUpper() + temp.Substring(1);
				int uIndex = -1;
				while (temp.IndexOf("_", 0, temp.Length) > 0) {
					uIndex = temp.IndexOf("_", 0, temp.Length);
					if (uIndex > 0) {
						string part1 = temp.Substring(0, uIndex);
						string part2 = temp.Substring(uIndex + 1);
						temp.Remove(uIndex, 1);

						//look for ID at the end of the string and make upper case
						if (part2 == "id") {
							part2 = part2.ToUpper();
						}
						else {
							part2 = part2.Substring(0, 1).ToUpper() + part2.Substring(1);
						}

						temp = part1 + part2;
					}
				}
			}
			else if (temp.Length > 0) {
				temp = temp.Substring(0, 1).ToUpper() + temp.Substring(1);
			}

			return temp;
		}

		public static string GetSpacedName(string s) {
			StringBuilder spacedName = new StringBuilder();

			for (int i = 0; i < s.Length; i++) {
				if (i > 0 && i < s.Length - 1 && s.Substring(i, 1).ToUpper() == s.Substring(i, 1)) {
					spacedName.Append(" ");
				}
				spacedName.Append(s[i]);
			}

			return spacedName.ToString();
		}
	}
}
